/**
 * ModCreditsPopUp.java
 * 
 * @author	Joe Scharnitzke
 * Date: 	Jun 2, 2011
 * 
 * This class defines the GUI for the small pop-up window that
 * appears when you attempt to modify the number of credits that
 * a character has in his/her possession.
 *
 * Change Log:
 * 	06/10/11 13:51 - (v1.1)
 * 						Functionality
 * 						> changed to a modal JDialog
 * 	06/10/11 10:57 - (v1.0) Release
 * 	Jun 2, 2011 11:12:51 AM - (v0.1) Initial Implementation
 */

package gui;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import ds.Character;

public class ModCreditsPopUp extends JDialog implements ActionListener
{
    private static final long serialVersionUID = 8322628699327694991L;
    
	private JButton jb_add_creds = new JButton("Add");
	private JTextField jtf_add_creds = new JTextField();
	private JButton jb_parent;
	
	private GridBagConstraints gbc_main = new GridBagConstraints();
	
	private Character ch_data;
	
	/**
	 * Two parameter constructor for ModCreditsPopUp
	 * objects.
	 * 
	 * @param init_ch_data the Character to add creds to
	 * @param init_jb_parent the parent button of this popup
	 */
	
	ModCreditsPopUp(Character init_ch_data, JButton init_jb_parent)
	{
		setLayout(new GridBagLayout());
		setTitle("Add Credits");
		
		ch_data = init_ch_data;
		jb_parent = init_jb_parent;
		
		jb_add_creds.addActionListener(this);
		jtf_add_creds.addActionListener(this);
		
		//add jtf_add_creds to this
		gbc_main.fill = GridBagConstraints.HORIZONTAL;
		gbc_main.gridx = 0;
		gbc_main.gridy = 0;
		gbc_main.insets = new Insets(5, 15, 5, 15);
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 1.0;
		add(jtf_add_creds, gbc_main);
		
		//add jb_add_creds to this
		gbc_main.fill = GridBagConstraints.HORIZONTAL;
		gbc_main.gridx = 0;
		gbc_main.gridy = 1;
		gbc_main.insets = new Insets(5, 15, 5, 15);
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 1.0;
		add(jb_add_creds, gbc_main);
		
		pack();
		setModal(true);
		GUIHelper.Center(this);
		setVisible(true);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource().equals(jb_add_creds) || event.getSource().equals(jtf_add_creds))
		{
			try
			{
				int i_creds = Integer.parseInt(jtf_add_creds.getText());
				ch_data.setICreds(ch_data.getICreds() + i_creds);
				jb_parent.setText(ch_data.getICreds() + " Credits");
				jb_parent.revalidate();
				dispose();
			}
			
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Invalid input.");
			}
			
			catch(Exception e)
			{
				System.err.println(e);
			}
		}
	}

}
